import request from '@/utils/request'

// 用户相关接口
export const userApi = {
  // 登录
  login: (data) => request.post('/user/login', data),
  // 获取用户信息
  getUserInfo: () => request.get('/user/info'),
  // 获取用户列表
  getUserList: (params) => request.get('/user/list', { params }),
  // 添加用户
  addUser: (data) => request.post('/user/add', data),
  // 更新用户
  updateUser: (data) => request.put(`/user/update/${data.id}`, data),
  // 删除用户
  deleteUser: (id) => request.delete(`/user/delete/${id}`),
  // 获取操作日志
  getOperationLogs: (params) => request.get('/user/logs', { params })
}

// 车辆相关接口
export const vehicleApi = {
  // 获取车辆列表
  getVehicleList: (params) => request.get('/api/vehicle/list', { params }),
  // 获取车辆详情
  getVehicleDetail: (id) => request.get(`/api/vehicle/${id}`),
  // 添加车辆
  addVehicle: (data) => request.post('/api/vehicle', data),
  // 更新车辆
  updateVehicle: (data) => request.put(`/api/vehicle`, data),
  // 删除车辆
  deleteVehicle: (id) => request.delete(`/api/vehicle/${id}`),
  // 获取车辆类型列表
  getVehicleTypes: () => request.get('/api/vehicle/types'),
  // 获取可用车辆
  getAvailableVehicles: () => request.get('/api/vehicle/available'),
  // 导出车辆数据 - 返回完整下载URL
  exportVehicles: (params) => {
    // 将参数转换为查询字符串
    const queryParams = [];
    if (params.searchTerm) queryParams.push(`searchTerm=${encodeURIComponent(params.searchTerm)}`);
    if (params.vehicleType) queryParams.push(`vehicleType=${encodeURIComponent(params.vehicleType)}`);
    if (params.status !== undefined && params.status !== null) queryParams.push(`status=${params.status}`);
    
    // 添加时间戳防止缓存
    queryParams.push(`_t=${Date.now()}`);
    
    // 返回完整的URL
    return `/api/vehicle/list/download?${queryParams.join('&')}`;
  },
  // 下载车辆导入模板
  downloadImportTemplate: () => {
    // 返回模板下载URL
    return `/api/vehicle/import/template?_t=${Date.now()}`;
  },
  // 导入车辆数据
  importVehicles: (file) => {
    const formData = new FormData();
    formData.append('file', file);
    return request.post('/api/vehicle/import', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
  },
  // 测试API连接
  testConnection: () => request.get('/api/vehicle/test')
}

// 驾驶员相关接口
export const driverApi = {
  // 获取驾驶员列表
  getDriverList: (params) => request.get('/api/driver/list', { params }),
  // 获取驾驶员详情
  getDriverDetail: (id) => request.get(`/api/driver/${id}`),
  // 添加驾驶员
  addDriver: (data) => request.post('/api/driver', data),
  // 更新驾驶员
  updateDriver: (data) => request.put('/api/driver', data),
  // 删除驾驶员
  deleteDriver: (id) => request.delete(`/api/driver/${id}`),
  // 获取部门列表
  getDepartments: () => request.get('/api/driver/departments'),
  // 导出驾驶员数据
  exportDrivers: (params) => request.get('/api/driver/export', { 
    params,
    responseType: 'blob' 
  }),
  // 测试API连接
  testConnection: () => request.get('/api/driver/test')
}

// 加油卡相关接口
export const fuelCardApi = {
  // 获取加油卡列表
  getFuelCardList: (params) => request.get('/api/fuelcard/list', { params }),
  // 获取加油卡详情
  getFuelCardDetail: (id) => request.get(`/api/fuelcard/${id}`),
  // 添加加油卡
  addFuelCard: (data) => request.post('/api/fuelcard', data),
  // 更新加油卡
  updateFuelCard: (data) => request.put(`/api/fuelcard`, data),
  // 删除加油卡
  deleteFuelCard: (id) => request.delete(`/api/fuelcard/${id}`),
  // 充值记录
  getRechargeRecords: (params) => request.get('/api/fuelcard/recharge/list', { params }),
  // 添加充值记录
  addRechargeRecord: (data) => request.post('/api/fuelcard/recharge/add', data),
  // 更新充值记录
  updateRechargeRecord: (data) => request.put(`/api/fuelcard/recharge/update/${data.id}`, data),
  // 删除充值记录
  deleteRechargeRecord: (id) => request.delete(`/api/fuelcard/recharge/delete/${id}`),
  // 导出加油卡数据
  exportFuelCards: (params) => request.get('/api/fuelcard/export', { 
    params,
    responseType: 'blob' 
  }),
  // 测试API连接
  testConnection: () => request.get('/api/fuelcard/test')
}

// ETC卡相关接口
export const etcCardApi = {
  // 获取ETC卡列表
  getEtcCardList: (params) => request.get('/api/etccard/list', { params }),
  // 获取ETC卡详情
  getEtcCardDetail: (id) => request.get(`/api/etccard/${id}`),
  // 添加ETC卡
  addEtcCard: (data) => request.post('/api/etccard', data),
  // 更新ETC卡
  updateEtcCard: (data) => request.put(`/api/etccard`, data),
  // 删除ETC卡
  deleteEtcCard: (id) => request.delete(`/api/etccard/${id}`),
  // 充值记录
  getRechargeRecords: (params) => request.get('/api/etccard/recharge/list', { params }),
  // 添加充值记录
  addRechargeRecord: (data) => request.post('/api/etccard/recharge/add', data),
  // 更新充值记录
  updateRechargeRecord: (data) => request.put(`/api/etccard/recharge/update/${data.id}`, data),
  // 删除充值记录
  deleteRechargeRecord: (id) => request.delete(`/api/etccard/recharge/delete/${id}`),
  // 导出ETC卡数据
  exportEtcCards: (params) => request.get('/api/etccard/export', { 
    params,
    responseType: 'blob' 
  }),
  // 测试API连接
  testConnection: () => request.get('/api/etccard/test')
}

// 绑定关联接口
export const bindingApi = {
  // 获取绑定关联列表
  getBindingList: (params) => {
    // 直接使用传入的参数，不做任何转换
    return request.get('/api/binding/list', { params });
  },
  // 添加绑定关联
  addBinding: (data) => request.post('/api/binding', data),
  // 更新绑定关联
  updateBinding: (data) => request.put(`/api/binding`, data),
  // 删除绑定关联
  deleteBinding: (id) => request.delete(`/api/binding/${id}`),
  // 测试API连接
  testConnection: () => request.get('/api/binding/test'),
  // 检查车辆是否已绑定
  checkVehicleBinding: (vehicleId, excludeId) => request.get(`/api/binding/check-vehicle`, { 
    params: { vehicleId, excludeId }
  })
}

// 出车记录接口
export const vehicleUsageApi = {
  // 获取出车记录列表
  getUsageList: (params) => request.get('/api/vehicle-usage/list', { params }),
  // 添加出车记录
  addUsage: (data) => request.post('/api/vehicle-usage/add', data),
  // 更新出车记录
  updateUsage: (data) => request.put(`/api/vehicle-usage/update/${data.id}`, data),
  // 删除出车记录
  deleteUsage: (id) => request.delete(`/api/vehicle-usage/delete/${id}`),
  // 归还车辆
  returnVehicle: (data) => request.put(`/api/vehicle-usage/return/${data.id}`, data)
}

// 加油记录接口
export const fuelUsageApi = {
  // 获取加油记录列表
  getFuelList: (params) => request.get('/api/fuel-usage/list', { params }),
  // 添加加油记录
  addFuel: (data) => request.post('/api/fuel-usage/add', data),
  // 更新加油记录
  updateFuel: (data) => request.put(`/api/fuel-usage/update/${data.id}`, data),
  // 删除加油记录
  deleteFuel: (id) => request.delete(`/api/fuel-usage/delete/${id}`)
}

// ETC通行记录接口
export const etcUsageApi = {
  // 获取ETC通行记录列表
  getEtcList: (params) => request.get('/api/etc-usage/list', { params }),
  // 添加ETC通行记录
  addEtc: (data) => request.post('/api/etc-usage/add', data),
  // 更新ETC通行记录
  updateEtc: (data) => request.put(`/api/etc-usage/update/${data.id}`, data),
  // 删除ETC通行记录
  deleteEtc: (id) => request.delete(`/api/etc-usage/delete/${id}`)
}

// ETC通停记录接口
export const etcParkingApi = {
  // 获取ETC通停记录列表
  getParkingList: (params) => request.get('/api/etc-parking/list', { params }),
  // 添加ETC通停记录
  addParking: (data) => request.post('/api/etc-parking/add', data),
  // 更新ETC通停记录
  updateParking: (data) => request.put(`/api/etc-parking/update/${data.id}`, data),
  // 删除ETC通停记录
  deleteParking: (id) => request.delete(`/api/etc-parking/delete/${id}`)
}

// 违章记录接口
export const violationApi = {
  // 获取违章记录列表
  getViolationList: (params) => request.get('/api/violation/list', { params }),
  // 添加违章记录
  addViolation: (data) => request.post('/api/violation/add', data),
  // 更新违章记录
  updateViolation: (data) => request.put(`/api/violation/update/${data.id}`, data),
  // 删除违章记录
  deleteViolation: (id) => request.delete(`/api/violation/delete/${id}`)
}

// 事故记录接口
export const accidentApi = {
  // 获取事故记录列表
  getAccidentList: (params) => request.get('/api/accident/list', { params }),
  // 添加事故记录
  addAccident: (data) => request.post('/api/accident/add', data),
  // 更新事故记录
  updateAccident: (data) => request.put(`/api/accident/update/${data.id}`, data),
  // 删除事故记录
  deleteAccident: (id) => request.delete(`/api/accident/delete/${id}`)
}

// 保险记录接口
export const insuranceApi = {
  // 获取保险记录列表
  getInsuranceList: (params) => request.get('/api/insurance/list', { params }),
  // 添加保险记录
  addInsurance: (data) => request.post('/api/insurance/add', data),
  // 更新保险记录
  updateInsurance: (data) => request.put(`/api/insurance/update/${data.id}`, data),
  // 删除保险记录
  deleteInsurance: (id) => request.delete(`/api/insurance/delete/${id}`)
}

// 保养记录接口
export const serviceApi = {
  // 获取保养记录列表
  getServiceList: (params) => request.get('/api/service/list', { params }),
  // 添加保养记录
  addService: (data) => request.post('/api/service/add', data),
  // 更新保养记录
  updateService: (data) => request.put(`/api/service/update/${data.id}`, data),
  // 删除保养记录
  deleteService: (id) => request.delete(`/api/service/delete/${id}`)
}

// 年检记录接口
export const inspectionApi = {
  // 获取年检记录列表
  getInspectionList: (params) => request.get('/api/inspection/list', { params }),
  // 添加年检记录
  addInspection: (data) => request.post('/api/inspection/add', data),
  // 更新年检记录
  updateInspection: (data) => request.put(`/api/inspection/update/${data.id}`, data),
  // 删除年检记录
  deleteInspection: (id) => request.delete(`/api/inspection/delete/${id}`)
}

// 维修记录接口
export const repairApi = {
  // 获取维修记录列表
  getRepairList: (params) => request.get('/api/repair/list', { params }),
  // 添加维修记录
  addRepair: (data) => request.post('/api/repair/add', data),
  // 更新维修记录
  updateRepair: (data) => request.put(`/api/repair/update/${data.id}`, data),
  // 删除维修记录
  deleteRepair: (id) => request.delete(`/api/repair/delete/${id}`)
}

// 报表接口
export const reportApi = {
  // 车辆使用分析
  getUsageAnalysis: (params) => request.get('/api/report/usage-analysis', { params }),
  // 成本分析
  getCostAnalysis: (params) => request.get('/api/report/cost-analysis', { params }),
  // 违章事故分析
  getTrafficAccidentAnalysis: (params) => request.get('/api/report/traffic-accident', { params }),
  // 维护分析
  getMaintenanceAnalysis: (params) => request.get('/api/report/maintenance-analysis', { params })
}

// 首页仪表盘接口
export const dashboardApi = {
  // 获取首页统计数据
  getStatistics: () => request.get('/api/dashboard/statistics'),
  // 获取车辆状态概览
  getVehicleStatus: () => request.get('/api/dashboard/vehicle-status'),
  // 获取里程统计
  getMileageStats: () => request.get('/api/dashboard/mileage-stats'),
  // 获取待处理违章
  getPendingViolations: () => request.get('/api/dashboard/pending-violations'),
  // 获取即将到期保养
  getUpcomingMaintenance: () => request.get('/api/dashboard/upcoming-maintenance'),
  // 获取近期出车记录
  getRecentUsageRecords: () => request.get('/api/dashboard/recent-usage-records'),
  // 获取车辆使用趋势
  getVehicleUsageTrend: () => request.get('/api/dashboard/vehicle-usage-trend'),
  // 获取月度费用统计
  getMonthlyCostStats: () => request.get('/api/dashboard/monthly-cost-stats')
} 